package com.starview.star.compiler;

import com.starview.platform.data.type.Location;
import com.starview.star.compiler.util.PrettyPrintDisplay;
import com.starview.star.compiler.util.PrettyPrintable;

@SuppressWarnings("serial")
public class CompilerMessage implements PrettyPrintable
{
  private final String msg;
  private final Location[] locs;

  public CompilerMessage(String msg, Location... locs)
  {
    if (msg == null)
      throw new IllegalArgumentException("message must not be null");
    this.msg = msg;
    if (locs == null)
      this.locs = new Location[] {};
    else
      this.locs = locs;
  }
  
  public String getMsg()
  {
    return msg;
  }

  @Override
  public boolean equals(Object obj)
  {
    if (obj instanceof CompilerMessage) {
      CompilerMessage other = (CompilerMessage) obj;
      if (other.msg.equals(msg) && other.locs.length == locs.length) {
        for (int ix = 0; ix < locs.length; ix++)
          if (!locs[ix].equals(other.locs[ix]))
            return false;
        return true;
      }
    }
    return false;
  }

  @Override
  public void prettyPrint(PrettyPrintDisplay disp)
  {
    disp.append(msg);
    for (Location loc : locs) {
      disp.append("\n at ");
      loc.prettyPrint(disp);
    }
  }

  @Override
  public String toString()
  {
    return PrettyPrintDisplay.toString(this);
  }

  @Override
  public int hashCode()
  {
    int hash = 37 * msg.hashCode();
    for (Location loc : locs)
      hash = hash * 37 + loc.hashCode();
    return hash;
  }
}
